
const db = require('db');
const { toInsert } = require('tools/sqlString');

module.exports = async ctx => {
    const { connection, run } = db.init();

    try {
        await into(ctx, run);
        ctx.body = 'ok';
        ctx.status = 200;
    } catch (error) {
        ctx.status = 500;
        ctx.body = error
    }

    connection.end();
}

async function into(ctx, run) {
    const body = ctx.request.body;
    const sqlStr = `INSERT INTO client ${toInsert(body, {}, 'client')}`;
    const result = await run(sqlStr)
    if (result.err) {
        throw result.err
    }
}
